home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / mcu / cluster.arc / AS.C < prev    next >
Text File  |  1989-05-24  |  4KB  |  221 lines

  1. char mapdn();
  2. char *alloc();
  3. /*
  4.  *    as ---    cross assembler main program
  5.  */
  6. main(argc,argv)
  7. int    argc;
  8. char    **argv;
  9. {
  10.     char    **np;
  11.     char    *i;
  12.     FILE    *fopen();
  13.     int    j = 0;
  14.  
  15.     if(argc < 2){
  16.         printf("Usage: %s [files]\n",argv[j]);
  17.         exit(1);
  18.         }
  19.       Argv = argv;
  20.       initialize();
  21.       while ((*argv[j] != '-') && (j<argc))
  22.        j++;
  23.       N_files = j-1;
  24.      if (j < argc )
  25.       {
  26.       argv[j]++;
  27.       while (j<argc)
  28.        {
  29.        for (i = argv[j]; *i != 0; i++)
  30.          if ((*i <= 'Z') && (*i >= 'A'))
  31.            *i = *i + 32;
  32.        if (strcmp(argv[j],"l")==0)
  33.          Lflag = 1;
  34.        else if (strcmp(argv[j],"nol")==0)
  35.          Lflag = 0;
  36.        else if (strcmp(argv[j],"c")==0)
  37.           Cflag = 1;
  38.        else if (strcmp(argv[j],"noc")==0)
  39.          Cflag = 0;
  40.        else if (strcmp(argv[j],"s")==0)
  41.          Sflag = 1;
  42.        else if (strcmp(argv[j],"cre")==0)
  43.          CREflag = 1;
  44.         j++;
  45.        }
  46.       }
  47.     root = NULL;
  48.  
  49.     Cfn = 0;
  50.     np = argv;
  51.     Line_num = 0; /* reset line number */
  52.     while( ++Cfn <= N_files )
  53.         if((Fd = fopen(*++np,"r")) == NULL)
  54.             printf("as: can't open %s\n",*np);
  55.         else{
  56.             make_pass();
  57.             fclose(Fd);
  58.         }
  59.     if( Err_count == 0 ){
  60.         Pass++;
  61.         re_init();
  62.         Cfn = 0;
  63.         np = argv;
  64.         Line_num = 0;
  65.         while( ++Cfn <= N_files)
  66.             if((Fd = fopen(*++np,"r")) != NULL)
  67.                 {
  68.                 make_pass();
  69.                 fclose(Fd);
  70.                  }
  71.             if (Sflag == 1)
  72.               {
  73.                 printf ("\f");
  74.                 stable (root);
  75.               }
  76.             if (CREflag == 1)
  77.               {
  78.                 printf ("\f");
  79.                 cross (root);
  80.               }
  81.         fprintf(Objfil,"S9030000FC\n"); /* at least give a decent ending */
  82.         }
  83.   printf("\n\nNumber of errors %d\n",Err_count);
  84.     exit(Err_count);
  85. }
  86.  
  87. initialize()
  88. {
  89.     FILE    *fopen();
  90.     int    i = 0;
  91.  
  92. #ifdef DEBUG
  93.     printf("Initializing\n");
  94. #endif
  95.     Err_count = 0;
  96.     Pc      = 0;
  97.     Pass      = 1;
  98.     Lflag      = 0;
  99.     Cflag      = 0;
  100.     Ctotal      = 0;
  101.     Sflag      = 0;
  102.     CREflag   = 0;
  103.     N_page      = 0;
  104.     Line[MAXBUF-1] = NEWLINE;
  105.  
  106.     strcpy(Obj_name,Argv[1]); /* copy first file name into array */
  107.     do {
  108.         if (Obj_name[i]=='.')
  109.            Obj_name[i]=0;
  110.     }
  111.     while (Obj_name[i++] != 0);
  112.     strcat(Obj_name,".s19");  /* append .out to file name. */
  113.     if( (Objfil = fopen(Obj_name,"w")) == NULL)
  114.         fatal("Can't create object file");
  115.     fwdinit();    /* forward ref init */
  116.     localinit();    /* target machine specific init. */
  117. }
  118.  
  119. re_init()
  120. {
  121. #ifdef DEBUG
  122.     printf("Reinitializing\n");
  123. #endif
  124.     Pc    = 0;
  125.     E_total = 0;
  126.     P_total = 0;
  127.     Ctotal    = 0;
  128.     N_page    = 0;
  129.     fwdreinit();
  130. }
  131.  
  132. make_pass()
  133. {
  134.     char    *fgets();
  135.  
  136. #ifdef DEBUG
  137.     printf("Pass %d\n",Pass);
  138. #endif
  139.     while( fgets(Line,MAXBUF-1,Fd) != (char *)NULL ){
  140.         Line_num++;
  141.         P_force = 0;    /* No force unless bytes emitted */
  142.         N_page = 0;
  143.            if(parse_line())
  144.             process();
  145.         if(Pass == 2 && Lflag && !N_page)
  146.             print_line();
  147.         P_total = 0;    /* reset byte count */
  148.         Cycles = 0;    /* and per instruction cycle count */
  149.         }
  150.     f_record();
  151. }
  152.  
  153.  
  154. /*
  155.  *    parse_line --- split input line into label, op and operand
  156.  */
  157. parse_line()
  158. {
  159.     register char *ptrfrm = Line;
  160.     register char *ptrto = Label;
  161.     char    *skip_white();
  162.  
  163.     if((*ptrfrm == '*') || (*ptrfrm == '\n') || (*ptrfrm == ';'))
  164.         return(0);    /* a comment line */
  165.  
  166.   while( delim(*ptrfrm)== NO )
  167.       *ptrto++ = *ptrfrm++;
  168.     if(*--ptrto != ':')ptrto++;     /* allow trailing : */
  169.     *ptrto = EOS;
  170.  
  171.     ptrfrm = skip_white(ptrfrm);
  172.  
  173.     ptrto = Op;
  174.   if(*ptrfrm != ';')
  175.       while( delim(*ptrfrm) == NO)
  176.           *ptrto++ = mapdn(*ptrfrm++);
  177.     *ptrto = EOS;
  178.  
  179.     ptrfrm = skip_white(ptrfrm);
  180.  
  181.     ptrto = Operand;
  182.   if(*ptrfrm != ';')
  183.       while( *ptrfrm != NEWLINE )
  184.           *ptrto++ = *ptrfrm++;
  185.     *ptrto = EOS;
  186.  
  187. #ifdef DEBUG
  188.     printf("Label-%s-\n",Label);
  189.     printf("Op----%s-\n",Op);
  190.     printf("Operand-%s-\n",Operand);
  191. #endif
  192.     return(1);
  193. }
  194.  
  195. /*
  196.  *    process --- determine mnemonic class and act on it
  197.  */
  198. process()
  199. {
  200.     register struct oper *i;
  201.     struct oper *mne_look();
  202.  
  203.     Old_pc = Pc;        /* setup `old' program counter */
  204.     Optr = Operand;     /* point to beginning of operand field */
  205.  
  206.     if(*Op==EOS){        /* no mnemonic */
  207.         if(*Label != EOS)
  208.             install(Label,Pc);
  209.         }
  210.     else if( (i = mne_look(Op))== NULL)
  211.         error("Unrecognized Mnemonic");
  212.     else if( i->class == PSEUDO )
  213.         do_pseudo(i->opcode);
  214.     else{
  215.         if( *Label )install(Label,Pc);
  216.         if(Cflag)Cycles = i->cycles;
  217.         do_op(i->opcode,i->class);
  218.         if(Cflag)Ctotal += Cycles;
  219.         }
  220. }
  221.